package com.mju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.dao.VipDao;
import com.mju.pojo.Vip;
import com.mju.service.IVipService;
import com.mju.service.ex.DeleteException;
import com.mju.service.ex.InsertException;
import com.mju.service.ex.UpdateException;
import com.mju.utils.GetTheCurrentTimeUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Service
public class VipServiceImpl implements IVipService {
  @Autowired private VipDao vipDao;

  @Override
  public Boolean addNewVip(Vip vip,Integer day) {
    // 根据uid查询用户是否曾是VIP
    Vip vip1 = vipDao.getByUid(vip.getUid());
    String currentTime = GetTheCurrentTimeUtils.getTheStrCurrentTime();

    if (Objects.isNull(vip1)) {
      // 未曾开通过VIP，则添加新的VIP信息
      try {
        //计算VIP结束时间
        Date endTime = GetTheCurrentTimeUtils.getTheEndTime(currentTime, day);
        vip.setEndTime(endTime);
      } catch (ParseException e) {
        e.printStackTrace();
      }

      vip.setStatus(false);
      int result = vipDao.insert(vip);
      if (result!=1){
        throw new InsertException("新建VIP信息失败");
      }
      return result==1;
    }else {
      // 开通过，则修改VIP信息，和计算结束时间 0-有效，1-过期
      // vip续期
      Boolean result1 = vipDao.updateVIPStatus(vip.getUid(), false)>0;
      Boolean result2 = this.vipRenewal( day, vip.getUid());
      if (!result1 || !result2){
        throw new UpdateException("更新VIP状态失败");
      }
      return (result1&&result2);

    }
  }

  @Override
  public Vip getByUid(Integer uid) {
    return vipDao.getByUid(uid);
  }

  @Override
  public Boolean deleteVIP(Integer uid) {
    // 修改VIP信息 0-有效，1-过期
    boolean result = vipDao.updateVIPStatus(uid, true)>0;
    if (!result){
      throw new DeleteException("删除VIP数据失败");
    }
    return result;
  }

  @Override
  public Boolean updateVIPStatus(Integer uid,Boolean status) {
    boolean result = vipDao.updateVIPStatus(uid, status)>0;
    if (!result){
      throw new UpdateException("修改VIP状态失败");
    }
    return result;
  }

  @Override
  public Vip queryVipByUid(Integer uid) {
    //校验VIP是否过期，如果过期则修改状态为过期
    boolean result = this.doesTheVipExpire(uid);
    if (result){
      System.out.println("修改VIP状态为过期");
    }
    return vipDao.queryVipByUid(uid);
  }

  @Override
  public Boolean doesTheVipExpire(Integer uid) {
    Vip vip = getByUid(uid);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String vipTime = dateFormat.format(vip.getEndTime());

    String currentTime= GetTheCurrentTimeUtils.getTheStrCurrentTime();
    if(GetTheCurrentTimeUtils.compareTime(vipTime, currentTime)){
      //会员有效，校正状态
      this.updateVIPStatus(vip.getUid(),false);
      return true;
    }
    // 会员到期，更新VIP状态
    this.updateVIPStatus(vip.getUid(),true);
    return false;
  }


  @SneakyThrows
  @Override
  public Boolean vipRenewal(Integer uid, Integer day) {
    Vip vip = vipDao.getByUid(uid);
    Date time = this.doesTheVipExpire(uid) ? vip.getEndTime() : GetTheCurrentTimeUtils.getTheCurrentTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date endTime = GetTheCurrentTimeUtils.getTheEndTime(dateFormat.format(time), day);
    Integer result = vipDao.updateVIPEndTime(vip.getUid(), endTime);
    if (result==0){
      throw new UpdateException("修改VIP过期时间失败");
    }
    //续费成功
    return result>0;
  }

  @Override
  public IPage<Vip> queryAllVipInfo(Integer currentPage, Integer pageSize) {
    IPage page = new Page<>(currentPage, pageSize);
    vipDao.selectPage(page,null);
    return page;
  }

  @Override
  public IPage<Vip> conQueryAllVipInfo(Integer currentPage, Integer pageSize, Vip vip) {
    IPage page = new Page<>(currentPage, pageSize);
    LambdaQueryWrapper<Vip> lwq=new LambdaQueryWrapper<>();
    lwq.eq(!Objects.isNull(vip.getUid()), Vip::getUid, vip.getUid());
    lwq.eq(!Objects.isNull(vip.getStatus()),Vip::getStatus,vip.getStatus());
    vipDao.selectPage(page,lwq);
    return page;
  }
}
